class ImageController < ApplicationController

  def create
    status = ''
    info = ''
    @image = Image.new(create_params)
    if !@image.valid?
      status = 'error'
      info = {error:@image.errors.messages}
    else
      # check repeat
      @image.user_id  = session[:user_id]
      temp = Image.find_by(name:@image.name, user_id:@image.user_id)
      if temp != nil
        status = 'error'
        info = {error:'image has the same name with the previous'}
      else
        #  check role
        if session[:user_name] != 'admin'
          @image.source = '3rd-party'
        end
        if session[:user_name] == 'admin'
          @image.source = 'official'
        end
        status = 'ok'
        info = {success:true}
        @image.save
      end
    end

  
    render json: {status:status, type:'image', currentAuthority:'user', info:info}
  end

  # query by name 
  def query
    status = ''
    info = ''
    @name = query_params
    if @name == nil
      status = 'error'
      info = {error:'params is not correct!'}
    else
      data = Image.where("name like ?","%#{@name}%")
      status = 'ok'
      info = {data:data}
    end

    render json: {status:status, type:'image', currentAuthority:'user', info:info}
  end

   # query by id
  def detail
    status = ''
    info = ''
    @image_id = detail_params
    if @image_id == nil
      status = 'error'
      info = {error:'params is not correct!'}
    else
      data = Image.find_by(id: @image_id)
      status = 'ok'
      info = {data:data}
    end

    render json: {status:status, type:'image', currentAuthority:'user', info:info}
  end

  # list all/current 
  def list
    # @mode = list_params
    # if @mode == 'byuser' # list current user's images
    #   # @user_id = params[:id]
    #   if @user_id != nil
    #     data = Image.where(user_id:@user_id)
    #   else
    #     data = Image.where(user_id:session[:user_id])
    #   end
    #   status = ''
    #   info = ''
    #   status = 'ok'
    #   info = {data:data}
    # elsif @mode == 'all' # list all users' images
    #   status = ''
    #   info = ''
    #   data = Image.all
    #   status = 'ok'
    #   info = {data:data}
    # else
    #   status = 'error'
    #   info = {error:'params is not correct!'}
    # end
    status = ''
    info = ''
    data = Image.where(user_id:session[:user_id])
    status = 'ok'
    info = {data:data}
    render json: {status:status, type:'image', currentAuthority:'user', info:info}
  
  end

  def delete
    status = ''
    info = ''
    @target = Image.find_by(id:params[:id], name:params[:name], user_id:session[:user_id])
    if !@target
      status = 'error'
      info = {error:"Can't find any Image with id and name"}
    else
      status = 'ok'
      @target.destroy
      info = {success:true}
    end

    render json: {status:status, type:'image', currentAuthority:'user', info:info}
  end

  def update
    status = ''
    info = ''

    @target = Image.find_by(id:params[:id], name:params[:name], user_id:session[:user_id])
    if !@target
      status = 'error'
      info = {error:"Can't find any Image with id and name"}
    else
      status = 'ok'
      @target.update(name:params[:name], description:params[:description], url:params[:url])
      info = {success:true}
    end

    render json: {status:status, type:'image', currentAuthority:'user', info:info}
  end


  private 
  def create_params
    params.require(:image).permit(:name,:url,:description)
  end
  
  def query_params
    params.require(:name)
	end

  def detail_params
    params.require(:id)
  end

  def list_params
    params.require(:mode)
  end

  def update_params
    params.permit(:id, :name, :url, :description)
  end

  def delete_params
    params.permit(:id, :name)
  end

end
